//
//  GYRegisterViewController.m
//  research
//
//  Created by 章鱼 on 17/3/28.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYRegisterViewController.h"
#import "GYRegisterTextfieldView.h"
#import <YYText/YYText.h>
#import "GYConfigDBManager.h"
#import "GYLoginViewController.h"

@interface GYRegisterViewController ()
@property (nonatomic, strong) GYRegisterTextfieldView *userNameView;//用户名
@property (nonatomic, strong) GYRegisterTextfieldView *phoneNumberView;//手机
@property (nonatomic, strong) GYRegisterTextfieldView *emailNumberView;//邮箱
@property (nonatomic, strong) GYRegisterTextfieldView *passwordNumberView;//密码
@property (nonatomic, strong) GYRegisterTextfieldView *surepasswordNumberView;//确认密码
@property (nonatomic, strong) UIButton *slectedButton;
@property (nonatomic, strong) YYLabel *delegateLable;

@end

@implementation GYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self initRegisterView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];
    [self addRightButtonItemWithTitle:@"完成" backImg:@""];
}

- (void)rightButtonItemClick {
    [super rightButtonItemClick];
    [self rightButtonClicked];
}

- (void)basicSetup {
    [self initNavBarWithTitle:@"注册" tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:18],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];
}

- (void)initRegisterView {
    _userNameView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_userNameView];
    _userNameView.titleLable.text = @"姓名";
    _userNameView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入您的用户名" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _phoneNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_phoneNumberView];
    _phoneNumberView.titleLable.text = @"手机";
    
    _phoneNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入常用手机号码" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _emailNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_emailNumberView];
    _emailNumberView.titleLable.text = @"邮箱";
    _emailNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入常用电子邮箱" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];

    _passwordNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_passwordNumberView];
    _passwordNumberView.titleLable.text = @"密码";
    _passwordNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您输入2~9的密码" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];

    _surepasswordNumberView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_surepasswordNumberView];
    _surepasswordNumberView.titleLable.text = @"确认密码";
    _surepasswordNumberView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"为了保险请再来一遍" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
        [self initRegisterViewUI];
}

- (void)initRegisterViewUI {
    [_userNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(84.f);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_phoneNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_userNameView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_emailNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_phoneNumberView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_passwordNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_emailNumberView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_surepasswordNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passwordNumberView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
}

- (void)slectedButton:(UIButton *)btn {
    btn.selected = !btn.selected;
}

#pragma action
- (void)rightButtonClicked {
    if (_userNameView.textFiled.text.length == 0) {
        [VBAlertView showTextHUD:@"请输入姓名"];
        return;
    }
    if (_phoneNumberView.textFiled.text.length == 0) {
        [VBAlertView showTextHUD:@"请输入电话号码"];
        return;
    }
    if (_emailNumberView.textFiled.text.length == 0) {
        [VBAlertView showTextHUD:@"请输入邮箱"];
        return;
    }
    if (_passwordNumberView.textFiled.text.length == 0) {
        [VBAlertView showTextHUD:@"请输入密码"];
        return;
    }
    if (![_passwordNumberView.textFiled.text isEqualToString:_surepasswordNumberView.textFiled.text]) {
        [VBAlertView showTextHUD:@"请两次输入同样的密码"];
        return;
    }
    
    [[YGHttpManager defaultManager] fetchGetUserRegisterWithuserName:_userNameView.textFiled.text password:_passwordNumberView.textFiled.text email:_emailNumberView.textFiled.text phone:_phoneNumberView.textFiled.text success:^(id responseObj) {
        GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
        if (commonmodel.status == 0) {
            [VBAlertView showTextHUD:commonmodel.message];
        }
        if (commonmodel.status == 1) {
            
            GYUserInfoModel *userModel = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
            [[GYConfigDBManager defaultManager] setUserInformation:userModel];
            [[GYConfigDBManager defaultManager] setLogin:YES];
            [VBAlertView showTextHUD:@"注册成功"];
            GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
            [self presentViewController:nav animated:YES completion:NULL];
            
        }
    } failure:^(NSError *error) {
     
        
    }];
    
}

@end
